package com.sree.tali.actions;

import com.sree.tali.pojo.UploadImagePojo;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.DispatchAction;

public class UploadImages
  extends DispatchAction
{
  private static final String SUCCESS = "success";
  
  public ActionForward uploadImagePage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    String basepath = getServlet().getServletContext().getRealPath("/");
    List<String> list = new ArrayList();
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(getServlet().getServletContext().getRealPath("/") + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setFolders(list);
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward getSubfolders(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String basepath = getServlet().getServletContext().getRealPath("/") + uploadimagepojo.getFolderName();
    List<String> list = new ArrayList();
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(basepath + "\\" + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setSubFolders(list);
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward uploadImages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    System.out.println("images are ==== " + uploadimagepojo.getImageList().size());
    
    return mapping.findForward("uloadimages");
  }
}
